package com.ofek.eventos;

import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import com.ofek.applicationbase.utils.ReflectionUtils;

public class ListAdapter<T> extends ArrayAdapter<T> {
	private final BroadcastReceiver broadcastReciever = new BroadcastReceiver() {

		@Override
		public void onReceive(final Context context,
				final Intent intent) {
			notifyDataSetChanged();

		}

	};
	private final BroadcastReceiver editBroadcast = new BroadcastReceiver() {

		@Override
		public void onReceive(final Context context,
				final Intent intent) {
			setEditState(intent.getBooleanExtra(Constants.EXTRA_DATA, false));
		}

	};
	private boolean edit;

	public void onResume() {
		LocalBroadcastManager.getInstance(getContext()).registerReceiver(broadcastReciever, new IntentFilter(Constants.EVENT_NOTIFY_EVENT_SET_CHANGED));
		LocalBroadcastManager.getInstance(getContext()).registerReceiver(editBroadcast, new IntentFilter(Constants.EDIT_REQUEST));
	}
	public void onPause() {
		LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(broadcastReciever);
		LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(editBroadcast);
	}

	public void setEditState(final boolean edit) {
		this.edit = edit;
		notifyDataSetInvalidated();
	}

	public void flipEditState() {
		edit = !edit;
		notifyDataSetInvalidated();
	}
	public ListAdapter(final Context context, final int resource, final int textViewResourceId,
			final List<T> objects) {
		super(context, resource, textViewResourceId, objects);
	}

	public ListAdapter(final Context context, final int resource, final int textViewResourceId,
			final T[] objects) {
		super(context, resource, textViewResourceId, objects);
	}

	public ListAdapter(final Context context, final int resource, final int textViewResourceId) {
		super(context, resource, textViewResourceId);
	}

	public ListAdapter(final Context context, final int resource, final List<T> objects) {
		super(context, resource, objects);
	}

	public ListAdapter(final Context context, final int resource, final T[] objects) {
		super(context, resource, objects);
	}

	public ListAdapter(final Context context, final int resource) {
		super(context, resource);
	}

	@Override
	public View getView(final int position, View convertView, final ViewGroup parent) {
		View delete = null;
		final T event = getItem(position);
		convertView = super.getView(position, convertView, parent);
		if (convertView == null) {
			final LayoutInflater inflater = (LayoutInflater) getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			try {
				convertView = inflater.inflate((Integer) ReflectionUtils.getValue(this,"mResource"), null);
			} catch (final Exception e1) {
				throw new RuntimeException(e1);
			}
		}

		delete = convertView.findViewById(R.id.delete);
		delete.setVisibility(getEdit() ? View.VISIBLE : View.INVISIBLE);
		delete.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(final View v) {
				deleteItem(event);
			}
		});
		onViewRequested(convertView);
		return convertView;
	}
	/**
	 * process view before showing it
	 * @param convertView
	 */
	protected void onViewRequested(final View convertView) {
	}
	/**
	 * @param event
	 */
	protected void deleteItem(final T event) {
		remove(event);
		notifyDataSetChanged();
	}
	/**
	 * @return 
	 */
	protected boolean getEdit() {
		return edit;
	}

}
